// uniCloud-aliyun/cloudfunctions/zhengaidan/index.obj.js (无排序版本)
const db = uniCloud.database();
const dbCmd = db.command;

module.exports = {
    _before: function () {},

    addOrder: async function(data) {
        return await db.collection('zhenggaidan').add(data);
    },

    delete: async function(id) {
        return await db.collection('zhenggaidan').doc(id).remove();
    },

    update: async function(params) {
        const { id, data } = params;
        return await db.collection('zhenggaidan').doc(id).update(data);
    },

    get: async function(id) {
        return await db.collection('zhenggaidan').doc(id).get();
    },

    getList: async function(params = {}) {
        // 移除了 orderBy 的默认值
        const { filter = {}, page = 1, pageSize = 20 } = params;
        let query = db.collection('zhenggaidan');
        let where = {};
        
        if (filter.plantId) {
            where.plantId = filter.plantId;
        }
        if (filter.waterPlantName) {
            where.waterPlantName = new RegExp(filter.waterPlantName, 'i');
        }
        if (filter.status) {
            where.status = filter.status;
        }

        const res = await query.where(where)
                              // .orderBy(orderBy) // 移除了排序
                              .skip((page - 1) * pageSize)
                              .limit(pageSize)
                              .get({ getCount: true });
        return res;
    }
}